import fs from "fs";
import csvParser from "csv-parser";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

const noise = [
  1, 2, 3, 4, 5, 6, 7, 8, 8.95, 9, 9.05, 9.1, 9.15, 9.2, 9.25, 9.3, 9.35, 9.4,
  9.45, 9.5, 9.55, 9.6, 9.65, 9.7, 9.75, 9.8, 9.85,
];
for (let groupNum = 2; groupNum <= 99; groupNum++) {
  const results = [];
  fs.createReadStream(`./lubang${groupNum}.csv`)
    .pipe(csvParser())
    .on("data", (row) => {
      results.push(row);
    })
    .on("end", async () => {
      for (let index = 0; index < results.length; index++) {
        const dataset = await prisma.dataset.findUnique({
          where: {
            name: `test${groupNum}_noise_${noise[index] * 2}`,
          },
        });
        await prisma.testResult.create({
          data: {
            datasetId: dataset.id,
            indicator1: parseFloat(results[index]["mAP_0.5"]),
            indicator2: parseFloat(results[index]["mAP_50_95"]),
          },
        });
      }
    });
}

const results = [];
fs.createReadStream(`./lubang1000.csv`)
  .pipe(csvParser())
  .on("data", (row) => {
    results.push(row);
  })
  .on("end", async () => {
    for (let index = 0; index < results.length; index++) {
      const dataset = await prisma.dataset.findUnique({
        where: {
          name: `standard_noise_${noise[index] * 2}`,
        },
      });
      await prisma.testResult.create({
        data: {
          datasetId: dataset.id,
          indicator1: parseFloat(results[index]["mAP_0.5"]),
          indicator2: parseFloat(results[index]["mAP_50_95"]),
        },
      });
    }
  });
